package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ModuleTreemain;
import com.ruoyi.system.service.IModuleTreemainService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 档案类别Controller
 * 
 * @author jiang
 * @date 2023-09-04
 */
@RestController
@RequestMapping("/system/treemain")
public class ModuleTreemainController extends BaseController {
    @Autowired
    private IModuleTreemainService moduleTreemainService;

    /**
     * 查询档案类别列表
     */
    // @PreAuthorize("@ss.hasPermi('system:treemain:list')")
    @GetMapping("/list")
    public AjaxResult list(ModuleTreemain moduleTreemain) {
        List<ModuleTreemain> list = moduleTreemainService.selectModuleTreemainList(moduleTreemain);
        return success(list);
    }

    /**
     * 导出档案类别列表
     */
    // @PreAuthorize("@ss.hasPermi('system:treemain:export')")
    @Log(title = "档案类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ModuleTreemain moduleTreemain) {
        List<ModuleTreemain> list = moduleTreemainService.selectModuleTreemainList(moduleTreemain);
        ExcelUtil<ModuleTreemain> util = new ExcelUtil<ModuleTreemain>(ModuleTreemain.class);
        util.exportExcel(response, list, "档案类别数据");
    }

    /**
     * 获取档案类别详细信息
     */
    // @PreAuthorize("@ss.hasPermi('system:treemain:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(moduleTreemainService.selectModuleTreemainById(id));
    }

    /**
     * 新增档案类别
     */
    // @PreAuthorize("@ss.hasPermi('system:treemain:add')")
    @Log(title = "档案类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ModuleTreemain moduleTreemain) {
        Long num = moduleTreemainService.selectMaxIndexId() + 1;
        moduleTreemain.setIndexId(num);

        Long indexid = moduleTreemain.getParentId();
        ModuleTreemain temp = new ModuleTreemain();
        temp.setIndexId(indexid);
        List<ModuleTreemain> list = moduleTreemainService.selectModuleTreemainList(temp);
        String id = list.get(0).getId() + num + "|";
        moduleTreemain.setId(id);

        return toAjax(moduleTreemainService.insertModuleTreemain(moduleTreemain));
    }

    /**
     * 修改档案类别
     */
    // @PreAuthorize("@ss.hasPermi('system:treemain:edit')")
    @Log(title = "档案类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ModuleTreemain moduleTreemain) {
        return toAjax(moduleTreemainService.updateModuleTreemain(moduleTreemain));
    }

    /**
     * 删除档案类别
     */
    // @PreAuthorize("@ss.hasPermi('system:treemain:remove')")
    @Log(title = "档案类别", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(moduleTreemainService.deleteModuleTreemainByIds(ids));
    }
}
